package com.wiley.searchPO;

import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import java.net.MalformedURLException;

public class CategoryPage extends BasePage {
	
	LoggerControler log;

	/**
	 * 页面元素
	 */
	//分类
	public static final By category_loc = By.id(BaseId + "bar_category");
	
	//主食
	public static final By staple_loc = By.name("主食");
	
	//iv_cover
	public static final By iv_cover_loc = By.id(BaseId + "iv_cover");
	
	//详情页面title_tv
	public static final By title_tv_loc = By.id(BaseId + "title_tv");
	
	//返回
	public static final By back_loc = By.name("转到上一层级");
	
	/**
	 * 操作方法
	 */
	//点击分类
	public void clickCategory(){
		this.clickEle(category_loc);
	}
	
	//点击主食
	public void clickStaple(){
		this.clickEle(staple_loc);
	}
	
	//循环访问主食菜单
	public void clickCategoryMenu() throws InterruptedException{
		this.clickStaple();
		log.info("点击主食");
		String[] menu = {"炒饭","粥","面食","粉","饼","寿司","饭","盖浇饭","煲仔饭","石锅拌饭","面","炒面","馒头","饺子","包子","卷子","糕点","汤圆","混沌"};
		for(int i = 0; i < menu.length; i++){
			try{
				this.clickEle(By.name(menu[i]));
				log.info("点击第"+(i+1)+"个主食："+menu[i].toString());
				int coverSize = this.getElesLength(iv_cover_loc);
				for(int j = 0; j < coverSize; j++){
					this.clickEles(iv_cover_loc, j);
					log.info("点击第"+(j+1)+"个cover");
					log.info("该封面title为"+this.getTitleName());
					driver.sendKeyEvent(4);
				}
				if(this.isElementPresent(back_loc)){
					this.clickEle(back_loc);
				}
			}catch(Exception e){
				Thread.sleep(1500);
			}
		}
	}

	//获取标题
	public String getTitleName(){
		WebElement ele = this.findEle(title_tv_loc);
		return ele.getText();
	}
	/**
	 * 业务封装
	 * @throws MalformedURLException 
	 * @throws InterruptedException 
	 */
	public void categoryView(Class name) throws MalformedURLException, InterruptedException  {
		log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		this.clickCategory();
		log.info("点击进入好豆分类列表");
		this.clickCategoryMenu();
		log.info("该case测试通过！");
	}
}
